package com.cxyzy.alertdialogdemo

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button.setOnClickListener { showDialog() }
    }

    /**
     * 显示提示对话框
     */
    private fun showDialog() {
        AlertDialog.Builder(this)
            .setTitle("对话框标题")
            .setMessage("对话框内容")
            .setIcon(R.mipmap.ic_launcher)//设置图标
            .setCancelable(true) //设置点击对话框以外区域是否让对话框消失
            //设置确定按钮
            .setPositiveButton("确定") { dialog, _ ->
                Toast.makeText(this, "你点击了确定", Toast.LENGTH_SHORT).show()
                dialog.dismiss()
            }
            //设置取消按钮
            .setNegativeButton("取消") { dialog, _ ->
                Toast.makeText(this, "你点击了取消", Toast.LENGTH_SHORT).show()
                dialog.dismiss()
            }
            .create().show()
    }

}
